import {
	reactive,
	ref
} from "vue";
import {
	arrayGet,
	arrayPluck,
	goSwitchTab,
	msg,
	objClear,
	validateArray
} from "../common";
import {
	InvitationToSign,
	userInvitationToSign,
	editInvitationToSign,
	tplStore,
	multiStore
} from "../../api/module/contract";
export const contractCommonRef = ref(null);
export const contractCommon = reactive({
	userType: 1,
	tpl_id: 0,
	data: {
		category: {
			id: 0,
			text: '',
		}, // 合同分类
		category_id: '', // 合同id
		recipient_id: [], // 抄送人
		recipient_members: [], // 抄送人
		audit_user: {}, // 企业审核人
		signer_user: {}, // 企业签署人
		title: '',
		end_date: '',
		remark: '',
		label: '',
		contract_file: [], //合同文件id
		contract_other_file: [], //合同附件
		contract_signer_file: [], //协议文件
		member: {
			key_word: "",
			remaining_quantity: 0,
		},
		signer: {
			key_word: "",
			user: []
		},
		is_annex: 2,
	},
	defaultData: {
		category: {
			id: 0,
			text: '',
		}, // 合同分类
		category_id: '', // 合同id
		title: '',
		end_date: '',
		remark: '',
		label: '',
		recipient_members: [],
		contract_file: [], //合同文件id
		contract_other_file: [], //合同附件
		contract_signer_file: [], //协议文件
		member: {
			key_word: "",
			cellphone: '',
			cellphone_secret: '',
			user_name: "",
			company_name: "",
			signer_user_type: ""
		},
		signer: {
			key_word: "",
			user: []
		},
		is_annex: 2,
	},
	rule: {
		title: [{
			required: true,
			message: '请输入主题',
			trigger: 'blur'
		}, ],
		end_date: [{
			required: true,
			message: '请选择签署截止日期',
			trigger: 'blur'
		}, ],
		contract_file: [{
			required: true,
			validator: validateArray,
			trigger: 'blur'
		}, ],
		member: [{
			required: true,
			validator: (rule, value, callback) => {
				if (!value.key_word) {
					callback(new Error('请添加签署关键字'));
				} else {
					callback();
				}
			},
			trigger: 'blur'
		}, ],
		signer: [{
			required: true,
			validator: (rule, value, callback) => {
				if (!value.user.length) {
					callback(new Error('请添加签署方'))
				}
				if (!value.key_word) {
					callback(new Error('请添加签署关键字'));
				} else {
					callback();
				}
			},
			trigger: 'blur'
		}, ],
	},
	reset: () => {
		contractCommon.tpl_id = 0;
		contractCommon.data = {
			category: {
				id: 0,
				text: '',
			}, // 合同分类
			category_id: '', // 合同id
			is_annex: 2,
			title: '',
			end_date: '',
			remark: '',
			label: '',
			recipient_members: [],
			signer_user: [],
			audit_user: [],
			contract_file: [], //合同文件id
			contract_other_file: [], //合同附件
			contract_signer_file: [], //协议文件
			member: {
				key_word: "",
				cellphone: '',
				cellphone_secret: '',
				user_name: "",
				company_name: "",
				signer_user_type: ""
			},
			signer: {
				key_word: "",
				user: []
			},
			is_annex: 2,
		};
	},
	clear: () => {
		contractCommon.data = contractCommon.defaultData;
	},
	sumbit: (userType) => {
		if (contractCommon.data.contract_file.length > 50) {
			return msg('签署文件数量超过最大数量')
		}
		let contract_file = arrayPluck(contractCommon.data.contract_file, 'id');
		let contract_signer_file = arrayPluck(contractCommon.data.contract_signer_file, 'id');
		let contract_other_file = arrayPluck(contractCommon.data.contract_other_file, 'id');
		let recipient_id = arrayPluck(contractCommon.data.recipient_members, 'id');
		let category_id = arrayGet(contractCommon.data.category, 'id');
		let audit_user_id = arrayGet(contractCommon.data.audit_user, 'id');
		let signer_user_id = arrayGet(contractCommon.data.signer_user, 'id');
		if (!contractCommon.data.title) {
			uni.$u.toast('请输入主题');
			return;
		}
		if (!contractCommon.data.end_date) {
			uni.$u.toast('请选择签署截止日期');
			return;
		}
		if (!contractCommon.data.member.key_word) {
			uni.$u.toast('请添加签署关键字');
			return;
		}
		if (contractCommon.data.signer.user.length == 0) {
			uni.$u.toast('请添加签署方');
			return;
		}
		if (!contractCommon.data.signer.key_word) {
			uni.$u.toast('请添加签署关键字');
			return;
		}
		if (contractCommon.data.contract_file.length == 0) {
			uni.$u.toast('请选择签署文件');
			return;
		}
		const params = {
			...contractCommon.data,
			contract_file: contract_file,
			contract_signer_file: contract_signer_file,
			contract_other_file: contract_other_file,
			recipient_id: recipient_id,
			category_id: category_id,
			audit_user_id: audit_user_id,
			signer_user_id: signer_user_id,
		};
		multiStore(params).then(res => {
			uni.$u.toast('发起合同成功');
			goSwitchTab('/pages/contract/index')
		});
	},
	edit: (userType) => {
		if (contractCommon.data.contract_file.length > 50) {
			return msg('签署文件数量超过最大数量')
		}
		let contract_file = arrayPluck(contractCommon.data.contract_file, 'id');
		let contract_signer_file = arrayPluck(contractCommon.data.contract_signer_file, 'id');
		let contract_other_file = arrayPluck(contractCommon.data.contract_other_file, 'id');
		let audit_user_id = arrayGet(contractCommon.data.audit_user, 'id');
		let signer_user_id = arrayGet(contractCommon.data.signer_user, 'id');
		if (!contractCommon.data.title) {
			uni.$u.toast('请输入主题');
			return;
		}
		if (!contractCommon.data.end_date) {
			uni.$u.toast('请选择签署截止日期');
			return;
		}
		if (!contractCommon.data.member.key_word) {
			uni.$u.toast('请添加签署关键字');
			return;
		}
		if (contractCommon.data.signer.user.length == 0) {
			uni.$u.toast('请添加签署方');
			return;
		}
		if (!contractCommon.data.signer.key_word) {
			uni.$u.toast('请添加签署关键字');
			return;
		}
		if (contractCommon.data.contract_file.length == 0) {
			uni.$u.toast('请选择签署文件');
			return;
		}
		// let msg = userType == 1 ? '请先实名认证' : '请先认证新企业'
		// if (validate.status_code == 501) {
		//     uni.$u.toast(msg)
		//     setTimeout(() => {
		//         goNav(arrayGet(pageUrl, `realnameUrl.${userType}`))
		//     }, 1500)
		// }
		let recipient_id = arrayPluck(contractCommon.data.recipient_members, 'id');
		let category_id = arrayGet(contractCommon.data.category, 'id');
		const params = {
			...contractCommon.data,
			contract_file: contract_file,
			contract_signer_file: contract_signer_file,
			contract_other_file: contract_other_file,
			recipient_id: recipient_id,
			category_id: category_id,
			audit_user_id: audit_user_id,
			signer_user_id: signer_user_id,
		};
		editInvitationToSign(params).then(res => {
			uni.$u.toast('编辑成功');
			goSwitchTab('/pages/contract/index')
		});
		// });
	},
	tpl: () => {
		if (contractCommon.data.contract_file.length > 50) {
			return msg('签署文件数量超过最大数量')
		}
		let recipient_id = arrayPluck(contractCommon.data.recipient_members, 'id');
		let contract_file = arrayPluck(contractCommon.data.contract_file, 'id');
		let contract_signer_file = arrayPluck(contractCommon.data.contract_signer_file, 'id');
		let contract_other_file = arrayPluck(contractCommon.data.contract_other_file, 'id');
		let category_id = arrayGet(contractCommon.data.category, 'id');
		let audit_user_id = arrayGet(contractCommon.data.audit_user, 'id');
		let signer_user_id = arrayGet(contractCommon.data.signer_user, 'id');
		contractCommonRef.value.validate().then(validate => {
			if (!validate) {
				return false;
			}
			const params = {
				...contractCommon.data,
				file: contract_file,
				signer_file: contract_signer_file,
				other_file: contract_other_file,
				id: contractCommon.tpl_id,
				category_id: category_id,
				recipient_id: recipient_id,
				audit_user_id: audit_user_id,
				signer_user_id: signer_user_id
			};
			params.name = params.title;
			params.file = params.contract_file;
			tplStore(params).then(res => {
				contractCommon.tpl_id = res.id;
				uni.$u.toast('保存成功');
			});
		});
	},
	tplAppend: (tpl_id, tplData) => {
		contractCommon.tpl_id = tpl_id;
		contractCommon.data.category_id = arrayGet(tplData, 'contract_tpl_category_id');
		contractCommon.data.category = {
			id: arrayGet(tplData, 'contract_tpl_category_id'),
			text: arrayGet(tplData, 'contract_tpl_category_name'),
		};
		contractCommon.data.recipient_members = [];
		contractCommon.data.contract_file = arrayGet(tplData, 'contract_tpl_file');
		contractCommon.data.contract_signer_file = arrayGet(tplData, 'contract_tpl_signer_file') || [];
		contractCommon.data.contract_other_file = arrayGet(tplData, 'contract_tpl_other_file');
		contractCommon.data.title = arrayGet(tplData, 'contract_tpl_title');
		contractCommon.data.end_date = uni.$u.timeFormat(arrayGet(tplData, 'contract_tpl_end_date'),
			'yyyy-mm-dd');
		contractCommon.data.remark = arrayGet(tplData, 'contract_tpl_remark');
		contractCommon.data.recipient_members = arrayGet(tplData, 'contract_tpl_recipient_members');
		contractCommon.data.signer_user = arrayGet(tplData, 'contract_tpl_signer_user');
		contractCommon.data.audit_user = arrayGet(tplData, 'contract_tpl_audit_user');
		if (arrayGet(tplData, 'contract_tpl_member')) {
			contractCommon.data.member = arrayGet(tplData, 'contract_tpl_member');
		}
		if (arrayGet(tplData, 'contract_tpl_member.key_word')) {
			contractCommon.data.member.key_word = arrayGet(tplData, 'contract_tpl_member.key_word');
		}
		if (arrayGet(tplData, 'contract_tpl_signer') != null) {
			contractCommon.data.signer = arrayGet(tplData, 'contract_tpl_signer') || [];
		}
		contractCommon.data.label = arrayGet(tplData, 'contract_tpl_label');
		contractCommon.data.is_annex = arrayGet(tplData, 'contract_tpl_is_annex');
	}
});